function OrgPanel(){
	
}

OrgPanel.instance = OrgPanel;

OrgPanel.faceUrl = "orgController";

OrgPanel.data = [];

OrgPanel.orgTreegrid = null;

OrgPanel.orgDatagrid = null;

OrgPanel.orgEditWindow = null;

OrgPanel.orgTreeselect = null;

OrgPanel.orgUserEditWindow = null;

OrgPanel.orgUserDatagrid = null;

OrgPanel.orgUserPager = null;

OrgPanel.init = function(){
	OrgPanel.loadToolbar();
	
	OrgPanel.orgTreegrid = mini.get("orgTreegrid");
	OrgPanel.orgDatagrid = mini.get("orgDatagrid");
	OrgPanel.orgEditWindow = mini.get("orgEditWindow");
	OrgPanel.orgTreeselect = mini.get("orgTreeselect");
	OrgPanel.orgUserEditWindow = mini.get("orgUserEditWindow");
	OrgPanel.orgUserDatagrid = mini.get("orgUserDatagrid");
	OrgPanel.orgUserPager = mini.get("orgUserPager");
	OrgPanel.reload();
};

//查询搜索
OrgPanel.loadOrgData = function() {

	$.ajax( {
		type: 'POST',
		dataType: 'json',
		cache: false,
		url: __root+"/"+OrgPanel.faceUrl,
		data: {
			method: "find"
		},
		success: function(json) {
			if (json) {
				OrgPanel.orgTreegrid.loadData(json);
				OrgPanel.data = json;
			}
			return;
		},
		error: function(obj, ertxt) {
			mini.alert("系统错误：" + ertxt);
		}
	});
};

/**
 * 加载按钮
 */
OrgPanel.loadToolbar = function() {
	var array = new Array("system_org_add", "system_org_edit", "system_org_del",
			"system_org_setuser", "system_org_deluser","system_org_reload");
	var array2 = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < array2.length; i++) {
		mini.get(array[i]).setVisible(array2[i]);
	}
};

/**
 * 加载组织机构下属用户表
 * @return
 */
OrgPanel.loadOrgUser = function(obj) {
	
	var org = obj.node ? obj.node : obj;
	if (org) {
		$.ajax( {
			type: 'POST',
			dataType: 'json',
			cache: false,
			url: __root+"/"+OrgPanel.faceUrl,
			data: {
				method: "getUserListByOrgId",
				orgId: org.orgId
			},
			success: function(json) {
				if (json && json.length > 0) {
					OrgPanel.orgDatagrid.loadData(json);
				} else {
					OrgPanel.orgDatagrid.loadData([]);
				}
				return;
			},
			error: function(obj, ertxt) {
				mini.alert("系统错误：" + ertxt);
			}
		});
	}
};

/**
 * 加载全部用户
 * @param pageIndex  页码
 * @param pageSize  每页多少条
 */
OrgPanel.loadUser = function(pageIndex, pageSize){
	
	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/"+'userController',
		cache: false,
		data: {
			method: "find",
			size: pageSize,
			pageNo: pageIndex
		},
		success: function(json) {
			if (json) {
				OrgPanel.orgUserPager.setTotalCount(json.rowCount);
				OrgPanel.orgUserDatagrid.loadData(json.resultList);
				return;
			}
			return;
		},
		error: function(obj, ertxt) { /* 失败回调函数.这里是网络失败, 比如500错误等 */
			//mini.hideMessageBox(messageId);
			mini.alert("初始化数据错误：" + ertxt);
		}
	});
};

/**
 * 加载数据
 */
OrgPanel.reloadUser = function(){
	OrgPanel.loadUser(OrgPanel.orgUserPager.pageIndex, OrgPanel.orgUserPager.pageSize);
};

//添加机构
OrgPanel.add = function(){
	
	if (!MiniuiTools.hasPurview("system_org_add")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var data = OrgPanel.orgTreegrid.getSelectedNode();
	var fdata = {
		'porgId': '0',
		'porgName': '顶层机构'
	};

	if (data) {
		fdata.porgId = data.orgId;
		fdata.porgName = data.name;
	}
	
	var form = new mini.Form("#orgEditform");
	form.clear();

	OrgPanel.orgTreeselect.setText(fdata.porgName);
	var textCls = mini.get("orgEditWindowPorgId");
	textCls.setValue(fdata.porgId);
	OrgPanel.orgTreeselect .setData(OrgPanel.data);
	OrgPanel.orgEditWindow.show();
};

//编辑机构
OrgPanel.edit = function(){
	
	if (!MiniuiTools.hasPurview("system_org_edit")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var rowData = OrgPanel.orgTreegrid.getSelectedNode();
	if(!rowData){
		mini.alert("请选中要编辑的机构！");
	}else{
		var porg = MiniuiTools.findTreeNode(rowData.porgId, OrgPanel.data);// FunNodePanel.findNode(data.pfunId);

		var form = new mini.Form("#orgEditform");
		form.clear();
		
		if (!porg) porg = {
			orgId: '0',
			name: 'ROOT'
		};
		
		rowData.porgName = porg.name;
		rowData.porgId = porg.orgId;
		
		form.setData(rowData);
		OrgPanel.orgTreeselect.setText(rowData.porgName);
		OrgPanel.orgTreeselect.setData(OrgPanel.data);
		OrgPanel.orgEditWindow.show();
	}
};

//删除
OrgPanel.del = function() {
	if (!MiniuiTools.hasPurview("system_org_del")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	var rowData = OrgPanel.orgTreegrid.getSelectedNode();

	if(!rowData){
		mini.alert("请选中要删除的记录！");
	}else{
		mini.confirm("确定要删除机构名称为  " + "<span style='color: red;'> " +rowData.name + "</span>" + "  的机构吗？", "删除机构",
	            function (action) {    
			  if (action == "ok") {
				  rowData.method = "delete";
				  rowData.lslUser = gbOperateUser;
				  rowData.lslAddress = gbOperateIp;
				  
					$.ajax( {
						type: 'POST',
						dataType: 'json',
						url: __root+"/"+OrgPanel.faceUrl,
						data: rowData,
						success: function(text) {
							OrgPanel.reload();
							return;
						},
						error:function(){
							mini.alert("删除出错！");
						}
					});
			  }
		});
	}
};

//刷新
OrgPanel.reload = function(){
	OrgPanel.loadOrgData();
	OrgPanel.orgDatagrid.loadData([]);
};

//用户设置
OrgPanel.showUserSet = function(){
	
	var rowData = OrgPanel.orgTreegrid.getSelectedNode();
	if(!rowData){
		mini.alert("请选中要设置用户的机构！");
		return;
	}else{
		OrgPanel.reloadUser();
		OrgPanel.orgUserEditWindow.show();
	}
	
};

//页码改变时间
OrgPanel.pagechanged = function(e){
	OrgPanel.loadUser(e.pageIndex, e.pageSize);
};

/**
 * 关闭编辑机构功能窗口
 */
OrgPanel.closeOrgButton = function(){
	OrgPanel.orgEditWindow.hide();
};

/**
 * 关闭设置机构用户功能窗口
 */
OrgPanel.closeOrgUserButton = function(){
	OrgPanel.orgUserEditWindow.hide();
};

/**
 * 下拉列表值改变事件
 */
OrgPanel.treeSelectChanged = function(){
	var textCls = mini.get("orgEditWindowPorgId");
	var idValue = OrgPanel.orgTreeselect.getValue();
	
	textCls.setValue(idValue);
};

/**
 * 保存组织机构
 * @param as
 * @return
 */
OrgPanel.save = function(as) {
	var form = new mini.Form("#orgEditform");
	
	form.validate();
    if (form.isValid() == false) return;
	
	var data = form.getData();
	data.method = "save";

	if (as == "yes") data.orgId = "";
	if(data.isRoot == "true") data.porgId	= "0";
	
	data.lslUser = gbOperateUser;
	data.lslAddress = gbOperateIp;
	
	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/"+OrgPanel.faceUrl,
		data: data,
		success: function(text) {
			OrgPanel.reload();
			if (as != "yes") OrgPanel.closeOrgButton();
			return;
		}
	});
};

/**
 * 设置用户到组织机构
 */
OrgPanel.saveOrgUser = function() {
	
	var rowData = OrgPanel.orgUserDatagrid.getSelecteds();
	var org = OrgPanel.orgTreegrid.getSelectedNode();
	
	if(!rowData){
		mini.alert("请选中要设置的用户！");
		return;
	}
	
	var userId = [];
	
		for ( var i = 0; i < rowData.length; i++) {
			userId.push(rowData[i].userId);
		}
		var data = {
			method: 'saveUserOrg',
			orgId: org.orgId,
			userId: userId,
			lslUser: gbOperateUser,
			lslAddress: gbOperateIp
		};
		$.ajax( {
			type: 'POST',
			dataType: 'json',
			url: __root+"/"+OrgPanel.faceUrl,
			data: data,
			success: function(json) {
				OrgPanel.loadOrgUser(org);
				OrgPanel.closeOrgUserButton();
				return;
			},
			error: function(e, etxt) {
				mini.alert("系统错误：" + errtxt);
				OrgPanel.closeOrgUserButton();
				return;
			}
		});
};


/**
 * 删除组织机构用户
 * @return
 */
OrgPanel.delOrgUser = function() {
	var v = OrgPanel.orgDatagrid.getSelecteds();
	var org = OrgPanel.orgTreegrid.getSelectedNode();
	
	if (v && v.length > 0 && org) {
		var userId = [];
		for ( var i = 0; i < v.length; i++) {
			userId.push(v[i].userId);
		}
		var data = {
			method: 'delOrgUser',
			orgId: org.orgId,
			userId: userId,
			lslUser: gbOperateUser,
			lslAddress: gbOperateIp
		};
		
		$.ajax( {
			type: 'POST',
			dataType: 'json',
			url:  __root+"/"+OrgPanel.faceUrl,
			data: data,
			success: function(json) {
				OrgPanel.loadOrgUser(org);
				return;
			},
			error: function(e, etxt) {
				mini.alert("系统错误：" + etxt);
				return;
			}
		});
	} else {
		mini.alert("请选择要取消的机构用户！");
	}
};

